package service.impl;

import entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import repository.UserRepository;
import service.UserService;

import java.util.List;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserRepository userRepository;

    public Integer save(Object o) {
        User user = (User) o;
        return userRepository.save(user);
    }

    public List findAll() {
        return userRepository.findAll();
    }

    public void delete(Integer id) {
        userRepository.delete(id);
    }

    public Integer update(Object o) {
        User user = (User) o;
        userRepository.saveOrUpdate(user);
        return user.getId();
    }

    public User valid(String username, String pwd) {
        List users = userRepository.valid(username, pwd);
        if (users.size() == 1) {
            User u= (User) users.get(0);
            return u;
        } else {
            return null;
        }
    }
}
